package com.zlx.rulemk.datagen;

import com.typesafe.config.ConfigFactory;
import com.zlx.rulemk.marketing.beans.CDCLogBean;
import com.zlx.rulemk.marketing.beans.RuleTableRecord;
import com.alibaba.fastjson.JSON;
import com.zlx.rulemk.marketing.constant.ConfigNames;
import org.apache.commons.io.FileUtils;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * @Author: zhanglingxing
 * @CreateTime: 2022-07-13
 * @Description: 规则管理平台（web系统） 模拟器
 * @Version: 1.0
 */
public class _04_RuleManagerPlatformSimulator {

    public static void main(String[] args) throws Exception {

        Properties props = new Properties();
        props.setProperty("bootstrap.servers", ConfigFactory.load().getString(ConfigNames.KAFKA_BOOTSTRAP_SERVERS));
        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

        KafkaProducer<String, String> kafkaProducer = new KafkaProducer<>(props);

        // 将规则封装成CDCLogBean对象
        // CDCLogBean
        CDCLogBean insertRule = insertRule("rule3");

        // CDCLogBean insertRule = updateRule("rule3");

        // 将日志对象，转成JSON
        String rule = JSON.toJSONString(insertRule);

        ProducerRecord<String, String> record = new ProducerRecord<>(ConfigFactory.load().getString(ConfigNames.KAFKA_ACTION_RULE_TOPIC), rule);
        kafkaProducer.send(record);
        kafkaProducer.flush();

        System.out.println("模拟发送规则成功，ruleName=【rule3】");
    }

    // 插入新规则到mysql
    public static CDCLogBean insertRule(String ruleJsonName) throws Exception {
        // 读规则控制器的drools代码
        String drlString = FileUtils.readFileToString(new File("rule_engine/rules/"+ruleJsonName+".drl"), "utf-8");
        String jsonString = FileUtils.readFileToString(new File("rule_engine/rules/"+ruleJsonName+".json"), "utf-8");

        RuleTableRecord ruleTableRecord = new RuleTableRecord();
        ruleTableRecord.setRule_condition_json(jsonString);
        ruleTableRecord.setRule_controller_drl(drlString);
        ruleTableRecord.setAuthor("zlx");
        ruleTableRecord.setRule_name(ruleJsonName);
        ruleTableRecord.setRule_status("1"); //1代码规则可用
        ruleTableRecord.setId("1001");

        List<RuleTableRecord> data = new ArrayList<>();
        data.add(ruleTableRecord);


        CDCLogBean CDCLogBean = new  CDCLogBean();
        CDCLogBean.setData(data);
        CDCLogBean.setType("INSERT");

        return CDCLogBean;

    }

    // 插入新规则到mysql
    public static CDCLogBean updateRule(String ruleJsonName) throws Exception {
        // 读规则控制器的drools代码
        String drlString = FileUtils.readFileToString(new File("rule_engine/rules/"+ruleJsonName+".drl"), "utf-8");
        String jsonString = FileUtils.readFileToString(new File("rule_engine/rules/"+ruleJsonName+".json"), "utf-8");

        RuleTableRecord ruleTableRecord = new RuleTableRecord();
        ruleTableRecord.setRule_condition_json(jsonString);
        ruleTableRecord.setRule_controller_drl(drlString);
        ruleTableRecord.setAuthor("zlx");
        ruleTableRecord.setRule_name(ruleJsonName);
        ruleTableRecord.setRule_status("1"); //1代码规则可用
        ruleTableRecord.setId("1001");

        List<RuleTableRecord> data = new ArrayList<>();
        data.add(ruleTableRecord);


        CDCLogBean CDCLogBean = new  CDCLogBean();
        CDCLogBean.setData(data);
        CDCLogBean.setType("UPDATE");

        return CDCLogBean;

    }


    // 删除对应的规则
    public static CDCLogBean deleteRule(String ruleName)  {
        // 读规则控制器的drools代码
        RuleTableRecord ruleTableRecord = new RuleTableRecord();
        ruleTableRecord.setRule_name("ruleName");
        ruleTableRecord.setRule_status("0"); //1代码规则可用

        List<RuleTableRecord> data = new ArrayList<>();
        data.add(ruleTableRecord);

        CDCLogBean CDCLogBean = new  CDCLogBean();
        CDCLogBean.setData(data);
        CDCLogBean.setType("UPDATE");

        return CDCLogBean;

    }
}
